﻿using System;
using System.Linq;
using System.Web.Security;
using _2009035.Models;

namespace _2009035.Utilities
{
    public class HRRoleProvider : RoleProvider, IDisposable
    {
        DbEntities ex = new DbEntities();
        public override string[] GetRolesForUser(string username)
        {
            // connect to the role provider and match the username based on the roles.

            // check if the current user is an employee or he is the admin user
            int empNo = Convert.ToInt32(username);
            var currentUser = ex.Employees.FirstOrDefault(x => x.employeeNumber == empNo);

            return (currentUser == null) ? null : currentUser.ApplicationRoles.Select(x => x.roleName).ToArray();


        }



        public override void AddUsersToRoles(string[] empIds, string[] roleNames)
        {
            if ((empIds != null) && (roleNames != null))
            {
                if ((empIds.Length > 0) && (roleNames.Length > 0))
                {
                    for (int i = 0; i < empIds.Length; i++)
                    {
                        for (int j = 0; j < roleNames.Length; j++)
                        {
                            using (DbEntities ex = new DbEntities())
                            {
                                int empNo = Convert.ToInt32(empIds[i]);
                                string role = roleNames[i];
                                var currentUser = ex.Employees.FirstOrDefault(x => x.employeeNumber == empNo);
                                var newRole = ex.ApplicationRoles.First(x => x.roleName == role);
                                currentUser.ApplicationRoles.Add(newRole);
                                ex.SaveChanges();
                            }
                        }
                    }

                }

            }
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            //throw new NotImplementedException();
            if ((!string.IsNullOrEmpty(username)) && (!string.IsNullOrEmpty(roleName)))
            {
                try
                {
                    int employeeNumber = int.Parse(username);
                    var userInRole = ex.ApplicationRoles.FirstOrDefault(x => x.roleName == roleName).Employees.FirstOrDefault(x => x.employeeNumber == employeeNumber);
                    if (userInRole != null)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception)
                {

                    throw;
                }


            }

            return false;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}